package com.mlink.vs.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.seasar.struts.annotation.Execute;

import com.google.gson.Gson;
import com.mlink.vs.entity.PiccTaskFile;
import com.mlink.vs.service.PiccTaskFileService;
import com.mlink.vs.util.StreamUtil;

/**
 * 获取案件所有相关文件下载URI列表接口
 * @author Administrator
 */
public class ImgAction {

	/**
	 * request请求对象
	 */
	@Resource
	private HttpServletRequest req;

	/**
	 * response请求对象
	 */
	@Resource
	private HttpServletResponse rep;
	
	@Resource
	private PiccTaskFileService piccTaskFileService;
	
	@Execute(validator = false)
	public String list(){
		
		ServletContext context = req.getSession().getServletContext();
		final String scFileSavePath = context
				.getInitParameter("SERVER_URL_FOR_IMAGE");

		if (scFileSavePath == null) {
			// 环境变量未配置，异常退出
			System.out.println("环境变量[SERVER_URL_FOR_IMAGE]未配置，异常退出");
			return null;
		}
		
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		
		Map<String, String> stsMap = new HashMap<String, String>();

		gson = new Gson();
		Map<String, String> mapData = gson.fromJson(indata.toString(),Map.class);
		if (mapData == null)
			return null;
		
		String caseNumber = (String) mapData.get("caseNumber");
		String carNumber = (String) mapData.get("carNumber");
	
		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(caseNumber)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "caseNumber不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}

			if (StringUtils.isBlank(carNumber)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "carNumber不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			
			List<PiccTaskFile> piccTaskFileList = piccTaskFileService.findImg(caseNumber,carNumber);
			List imgs = new ArrayList();
			if(piccTaskFileList.size()>0){
				for (int i = 0; i < piccTaskFileList.size(); i++) {
					String fileUrl = piccTaskFileList.get(i).fileUrl;
					String replace = fileUrl.replace("\\","%2f").replace("/","%2f");
					String s=scFileSavePath+replace;
					imgs.add(s);
				}
				rep.getWriter().println(gson.toJson(imgs));
				rep.getWriter().flush();
			}else{
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@Execute(validator = false)
	public String showImg() {
		ServletContext context = req.getSession().getServletContext();
		final String scFileSavePath = context
				.getInitParameter("PHOTO_STORE_PATH");

		if (scFileSavePath == null) {
			// 环境变量未配置，异常退出
			System.out.println("环境变量[PHOTO_STORE_PATH]未配置，异常退出");
			return null;
		}

		String imagePath = (String) req.getParameter("sp");
		if (imagePath == null)
			return null;
		imagePath=URLDecoder.decode(imagePath);
		File imageFile = new File(scFileSavePath + imagePath);
		// System.out.println(" ******imageFile****** = " + imageFile.getAbsolutePath());
		
		if (!imageFile.exists())
			return null;
		
		// 根据文件扩展名，形成content-type
		String contentType = "application/octet-stream";
		if (imagePath.toLowerCase().endsWith("png")){
			contentType = "image/png"; 
		} else if (imagePath.toLowerCase().endsWith("jpg")){
			contentType = "image/jpeg";
		};
		rep.setHeader("Content-Type", contentType);
		
		// 读取图像文件,返回浏览器
		try {
			
			FileInputStream in = new FileInputStream(imageFile);
			ServletOutputStream out = rep.getOutputStream();
			byte [] buffer = new byte[1024]; 
			while(in.read(buffer) != -1){
				out.write(buffer);
			}
			in.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
		
		return null;
	}
}